import tkinter as tk
from tkinter import messagebox
from abc import ABC, abstractmethod

# 抽象基类，用于定义按钮行为
class ButtonAction(ABC):
    @abstractmethod
    def execute(self):
        pass

# 具体类，用于处理按钮A的默认行为
class DefaultAction(ButtonAction):
    def execute(self):
        messagebox.showinfo("输出", "Hello World!")

# 具体类，用于处理按下B后A的行为
class ActionAfterB(ButtonAction):
    def execute(self):
        messagebox.showinfo("输出", "执行B功能！")

# 具体类，用于处理按下C后A的行为
class ActionAfterC(ButtonAction):
    def execute(self):
        messagebox.showinfo("输出", "执行C功能！")

# 主控制器类，负责协调按钮和它们的行为
class Controller:
    def __init__(self, root):
        self.root = root
        self.current_action = DefaultAction()  # 初始行为

        # 创建按钮并绑定事件处理函数（使用lambda来传递self）
        self.button_a = tk.Button(root, text="A", command=lambda: self.on_button_a_click())
        self.button_a.pack(pady=10)

        self.button_b = tk.Button(root, text="B", command=self.on_button_b_click)
        self.button_b.pack(pady=10)

        self.button_c = tk.Button(root, text="C", command=self.on_button_c_click)
        self.button_c.pack(pady=10)

    def on_button_a_click(self):
        self.current_action.execute()

    def on_button_b_click(self):
        self.current_action = ActionAfterB()  # 更改当前行为

    def on_button_c_click(self):
        self.current_action = ActionAfterC()  # 更改当前行为

# 主程序入口
if __name__ == "__main__":
    root = tk.Tk()
    root.title("按钮控制器")

    # 创建控制器实例，并将窗口传递给它
    controller = Controller(root)

    # 进入Tkinter的主事件循环
    root.mainloop()